<%@ include file="shared_header.jsp" %>
<%@ page import="vrp.Partner" %>
<%@ page import="vrp.Customer" %>
<%@ page import="vrp.Distance" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
	
    query = "select from " + Partner.class.getName() + " where type==2 order by name asc";
    List<Partner> partners = (List<Partner>) pm.newQuery(query).execute();
%>

<script type="text/javascript" src="/scripts/customers.js"></script>

<div id="dialogmap" title="Map Display">
	<div id="map_canvas" style="width: 900px; height: 550px; border: 1px solid black;"></div>
</div>

<div style="padding: 0px 10px;">
<%
	List<Customer> customers;	
	
	String partnerIDStr = request.getParameter("whprovider");
	long partnerID = 0;
	if (partnerIDStr != null && !partnerIDStr.equals("") && !partnerIDStr.equals("0")) {
		partnerID = Integer.parseInt(partnerIDStr);
		Key partnerKey = KeyFactory.createKey(Partner.class.getSimpleName(), partnerID);
		query = "select from " + Customer.class.getName() + " where partner == partnerParam parameters String partnerParam order by name asc";
	    customers = (List<Customer>) pm.newQuery(query).execute(partnerKey);
	} else {
	    query = "select from " + Customer.class.getName() + " order by name asc";
	    customers = (List<Customer>) pm.newQuery(query).execute();
	}

    boolean depotsIsEmpty = true;
%>
	<h2>List of Depots <input type="button" id="buttonviewmap" value="View Map" /></h2>
	<div>
		<form target="_self" action="/depots.jsp">
			Warehouse Provider:&nbsp;
   			<select name="whprovider">
   				<option value="">All Warehouse Providers</option>
<%
   				for (Partner p : partners) {
%>			    			
			 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
      			}
%>							
			</select>
			<input type="Submit" value="Select" />
    	</form>
	</div>		
	<br/>	
	<table style="width: 100%;">
		<tr>
			<th>Name</th>
			<th>Address</th>
			<th>Postal Code</th>
			<th>Contact No</th>
			<th>Action</th>
		</tr>
		<tr style="display: none">
			<td id="pageIsDepot">true</td>
		</tr>
<%
    for (Customer c : customers) {
%>
		<tr style="display: none">
			<td class="marker">
				<div class="markerID"><%= c.getKey().getId() %></div>
				<div class="markerLat"><%= c.getLat() %></div>
				<div class="markerLng"><%= c.getLng() %></div>
				<div class="markerTitle"><%= c.getName() %></div>
				<div class="markerIsDepot"><%= c.getIsDepot() %></div>
			</td>
		</tr>
<%    	
       	if (c.getIsDepot()==true){
       		depotsIsEmpty = false;
%>
		<tr>
			<td><%= c.getName() %></td>
			<td><%= c.getAddress() %></td>
			<td><%= c.getPostalCode() %></td>
			<td><%= c.getContactNo() %></td>
			<td style="text-align:center;">
				<input type="button" value="Edit" class="buttonedit"/><br/>
				<input type="button" value="Delete" class="buttondelete"/>
			</td>
		</tr>
		<tr style="display: none;">
			<td colspan="5">
				<form action="/editcustomer" method="post">
					<div><b>Edit Depot</b></div>
		    		<div>Name:&nbsp;<input type="text" name="name" value="<%= c.getName() %>" /></div>
		    		<div>Address:&nbsp;<input type="text" name="address" value="<%= c.getAddress() %>" /></div>
		    		<div>Postal Code:&nbsp;<input type="text" name="postalCode" value="<%= c.getPostalCode() %>" /></div>
		    		<div>Contact No:&nbsp;<input type="text" name="contactNo" value="<%= c.getContactNo() %>" /></div>
		    		<input type="hidden" name="oldPostalCode" value="<%= c.getPostalCode() %>" />
					<input type="hidden" name="lat" value="0" />
		    		<input type="hidden" name="lng" value="0" />
		    		<input type="hidden" name="distances" value="" />
		    		<input type="hidden" name="key" value="<%= c.getKey().getId() %>"/>
					<input type="button" value="Edit" class="formsubmit editform" />
					<input type="button" value="Cancel" class="buttoncancel" />
				</form>
			</td>
		</tr>
		<tr style="display: none;">
			<td colspan="5">
				<form action="/deletecustomer" method="post">
					<div><b>Delete Depot</b></div>
		    		<div>This action would delete the above depot permanently. Proceed?</div>
					<input type="hidden" name="key" value="<%= c.getKey().getId() %>"/>
					<input type="submit" value="Delete" />
					<input type="button" value="Cancel" class="buttoncancel" />
				</form>
			</td>
		</tr>
<%
       	}
    }
	if (depotsIsEmpty == true) {
%>
		<tr><td colspan="5">There is no depot in the database.</td></tr>
<%
	}
    
%>
	</table>
	<input type="button" value="Add Depot" class="buttonadd" />
 		<div style="display: none;">
		<form action="/addcustomer" method="post">
			<div>Warehouse Provider:&nbsp;
    			<select name="partner">
<%
    				for (Partner p : partners) {
%>			    			
				 	<option value="<%= p.getKey().getId() %>" <%= ((partnerID == p.getKey().getId())?"selected":"") %>><%= p.getName() %></option>
<%
       			}
%>							
				</select>
    		</div>
    		<div>Name:&nbsp;<input type="text" name="name" /></div>
    		<div>Address:&nbsp;<input type="text" name="address" /></div>
    		<div>Postal Code:&nbsp;<input type="text" name="postalCode" /></div>
    		<div>Contact No:&nbsp;<input type="text" name="contactNo" /></div>
    		<input type="hidden" name="lat" value="0" />
    		<input type="hidden" name="lng" value="0" />
    		<input type="hidden" name="isDepot" value="true" />
    		<input type="hidden" name="distances" value="" />
    		<div><input type="button" value="Add" class="formsubmit" /><input type="button" value="Cancel" class="buttoncancel" /></div>
  		</form>
  	</div>
</div>
<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>